package com.lx.javasunmmrize.JUC.lock8;

import java.util.concurrent.TimeUnit;

/**
 * @Description 8锁问题1：先执行发信息还是打电话
 *
 * synchronized:锁的是方法的diaoyong者，谁先拿到谁先执行
 *              8锁问题2：AB线程分别执行同步方法和普通方法，哪个先执行？
 *
 *              TODO:为什么还行同步方法先执行？？？
 *
 * @Author lx
 * @Date 2021/3/3 0:39
 **/
public class Test01 {

    public static void main(String[] args) {
        Phone phone = new Phone();
        System.out.println(System.currentTimeMillis());
        new Thread(()->{
            phone.hello();
        },"B").start();


        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            phone.sentMsg();
        },"A").start();

    }
}


class Phone{

    public  synchronized  void sentMsg(){
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis() + "   发信息");
    }

    public  synchronized  void call(){
        System.out.println(System.currentTimeMillis() + "   打电话");
    }

    public void hello(){
        System.out.println(System.currentTimeMillis() + "   hello");
    }

}